//
// File:   comp_triggers.cpp
// Author: Kevin Gimpel
// Created on Feb 15, 2011
// Modified by VietThang
// Update on April 25, 2013
//

#include "comp_triggers.h"

int main(int argc, char** argv) {
    ParamsArray params(argc, argv);
    params.setDescription("compute trigger word pairs using Rosenfeld's method based on mutual information");
    params.addConstraint("input_file", "file containing tokenized input text, one sentence per line", false); // not optional
    params.addConstraint("output_file", "file in which to output trigger word pairs", false); // not optional
	params.addConstraint("min_word_count", "minimun word count (default: 5)", false);
	params.addConstraint("min_pair_count", "minimum pair count (default: 10)", false);
    params.addConstraint("min_gap_width", "minimum width of gap (in number of words) "
		"between words in each trigger pair (default: 1)", true); // optional
	params.addConstraint("max_window_size", "maximum distance between trigger and triggered word (in number of words) (default: 20)", true); // optional
    params.addConstraint("min_mi", "minimum mutual information of trigger pairs to print; if 0.0, will print all (default: 0.0)", true); // optional
	params.addConstraint("temp_dir", "temporary directory", true); //optional

   	if (!params.runConstraints("comp_triggers")) 
		return 0;

	// Read in command-line parameters.
	string inputFilename = params.asString("input_file");
	string outputFilename = params.asString("output_file");
	int offset = atoi(params.asString("min_gap_width", "1").c_str());
	int minWordCount = atoi(params.asString("min_word_count", "5").c_str());
	int minPairCount = atoi(params.asString("min_pair_count", "10").c_str());
	int maxWindowSize = atoi(params.asString("max_window_size", "20").c_str());
	double minMutualInformation = atof(params.asString("min_mi", "0.0").c_str());
	string dirTmp = params.asString("temp_dir");
	if (offset < 0) {
		cerr << "Minimum gap width must be >= 0. You passed in " << offset << ". Using zero instead..." << endl;
		offset = 0;
	}

	clock_t clock1, clock2;
	clock1 = clock();
	
	// ---
	int idx;
#ifdef WIN32
	idx = outputFilename.find_last_of('\\');
#else
	idx = outputFilename.find_last_of('/');
#endif
	string outputDir = outputFilename.substr(0, idx);
	//cerr << outputDir << endl;
	dictionary *dict;
#ifdef WIN32
	string outputFilenameDict = outputDir + "\\dict";
#else
	string outputFilenameDict = outputDir + "/dict";
#endif
	
	// Creating dictionary from training corpus
	dict = new dictionary(inputFilename);
	dict->save(outputFilenameDict, minWordCount);
	//dict->stat(dirTmp + "\\dictstat.chg.en");
	delete dict;

	// Load dictionary from disk
	dict = new dictionary(outputFilenameDict);
	
	// ---
	
	// ---
	// Computing and save trigger pairs
	mitrigger *miTable = new mitrigger(inputFilename, *dict, offset, maxWindowSize);
	miTable->compMI(outputFilename, minMutualInformation, minPairCount);
	// ---

	//// ---
	//// Statistic
	//string _outputFilename = dirTmp + "\\stat.chg.en";
	//miTable->stat(_outputFilename);
	//// ---

	//// ---
	//// Load and find trigger pairs
	//mitrigger *miTable = new mitrigger(inputFilename, dirTmp, offset);
	//string trigger = "I";
	//string triggered = "I";
	//double mivalue = miTable->findMI(trigger, triggered);
	//double pmivalue = miTable->findPMI(trigger, triggered);
	//cout << mivalue << "\t" << pmivalue << "\t" << trigger << "\t" << triggered << endl;
	//// ---

	clock2 = clock();
	double totalTime = (((double)(clock2 - clock1)) / ((double)(CLOCKS_PER_SEC)));
	cerr << "Total time: " << totalTime << " sec" << endl;
    return 0;
}